package ch02.jianDa;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileWriter;
import java.io.IOException;

public class StudentInfo {
    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerException {
        //工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //解析器
        DocumentBuilder db = dbf.newDocumentBuilder();
        //解析XML
        Document doc = db.parse("src/ch02/jianDa/Student.xml");
        //得到所有Student节点列表信息
        NodeList studentList = doc.getElementsByTagName("Student");
        //循环输出Student元素信息
        for (int i = 0; i < studentList.getLength(); i++) {
            Node student = studentList.item(i);//获取第i个student元素信息
            Element element = (Element) student;//获取第i个student元素的属性值
            String id = element.getAttribute("id");
            System.out.println("学号：" + id);
            NodeList nodes = element.getChildNodes();
            for (int j = 0; j < nodes.getLength(); j++) {
                if (nodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                    Element node = (Element) nodes.item(j);
                    if (node.getNodeName().equals("name")) {
                        System.out.println("\t姓名：" + node.getTextContent());
                    } else if (node.getNodeName().equals("course")) {
                        System.out.println("\t科目：" + node.getTextContent());
                    } else if (node.getNodeName().equals("score")) {
                        System.out.println("\t分数：" + node.getTextContent());
                    }
                }
            }
        }

        //添加
        Element docElement = doc.createElement("Student");
        docElement.setAttribute("id","3");
        Element name = doc.createElement("name");
        name.setTextContent("啊啊啊");
        Element course = doc.createElement("course");
        name.setTextContent("数学");
        Element score = doc.createElement("score");
        name.setTextContent("80");
        docElement.appendChild(name);
        docElement.appendChild(course);
        docElement.appendChild(score);
        Element scores = (Element) doc.getElementsByTagName("scores").item(0);
        scores.appendChild(docElement);

        for (int i = 0; i < studentList.getLength(); i++) {
            Element student = (Element) studentList.item(i);//获取第i个student元素信息
            String id = student.getAttribute("id");
            NodeList nodes = student.getChildNodes();
            //删除
            if (id.equals("1")) {
                for (int j = 0; j < nodes.getLength(); j++) {
                    Element node = (Element) nodes.item(j);
                    if (node.getNodeName().equals("score")){
                        node.getParentNode().removeChild(node);
                    }
                }
            }else if (id.equals("2")){
                //修改
                for (int j = 0; j < nodes.getLength(); j++) {
                    Element node = (Element) nodes.item(j);
                    if (node.getNodeName().equals("score")) {
                        node.setTextContent("60");
                    }
                }
            }
        }
        //保存XML文件
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
        StreamResult streamResult = new StreamResult(new FileWriter("src/ch02/jianDa/Student.xml"));
        //把DOM树转换为XML文件
        transformer.transform(domSource,streamResult);

    }
}
